package com.share.sharedevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.sharedevice.entity.Cabinet;
import com.share.sharedevice.entity.CabinetSlot;
import com.share.sharedevice.mapper.CabinetSlotMapper;
import com.share.sharedevice.service.ICabinetSlotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class CabinetSlotServiceImpl extends ServiceImpl<CabinetSlotMapper,CabinetSlot> implements ICabinetSlotService {


    @Autowired
    private CabinetSlotMapper cabinetSlotMapper;

    /**
     * 新增
     * @param cabinetSlot
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Cabinet cabinet, CabinetSlot cabinetSlot) {

        Long id = cabinet.getId();

        if (id == null){
            throw new RuntimeException("请选择充电宝柜机");
        }

        Integer totalSlots = cabinet.getTotalSlots();
        for (int i = 1; i <= totalSlots; i++) {
            cabinetSlot.setCabinetId(cabinet.getId());
            String s = String.valueOf(i);
            cabinetSlot.setSlotNo(s);
            cabinetSlot.setStatus("0");
            cabinetSlot.setDelFlag("0");
            cabinetSlot.setCreateTime(new Date());
            cabinetSlot.setUpdateTime(new Date());
            cabinetSlotMapper.add(cabinetSlot);
        }


    }

    /**
     * 物理删除
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteALL(Long id) {
        cabinetSlotMapper.deleteALL(id);
    }
}
